package com.coocaa.ops.admin.v1.audit;


import com.coocaa.ops.admin.base.annotation.AuditOps;
import com.coocaa.ops.admin.base.annotation.Auth;
import com.coocaa.ops.admin.base.annotation.PutShelvesOps;
import com.coocaa.ops.admin.base.controller.MgrBaseController;
import com.coocaa.ops.admin.v1.audit.model.ExecutionTaskModel;
import com.coocaa.ops.admin.v1.audit.service.IAuditTaskService;
import com.coocaa.ops.admin.v1.audit.validator.AuditTaskValidator;
import com.coocaa.ops.common.core.base.enums.AuthEnum;
import com.coocaa.ops.common.core.base.model.R;
import com.coocaa.ops.common.core.base.model.ResourceBaseModel;
import com.coocaa.ops.common.core.biz.audit.entity.AuditTaskInfoHistoryEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author bijiahao
 * @date : 2018/6/14.
 * @description: 审核前校验-审核-撤销审核-上架操作 前端控制器
 *
 */

@RestController
@RequestMapping("/v1/audit")
public class AuditTaskControllerV1 extends MgrBaseController {
    @Autowired
    private IAuditTaskService auditTaskService;


    /**
     * 上架操作
     *
     * @param resourceBaseModel
     * @param request
     * @return
     */
    @PutShelvesOps
    @Auth(verifyAuthority = true, authorityType = AuthEnum.AUDIT)
    @RequestMapping(value = "/putShelves", method = {RequestMethod.GET, RequestMethod.POST})
    public Object putShelves(ResourceBaseModel resourceBaseModel, HttpServletRequest request) {
        AuditTaskValidator.validIdAndVersionAndType(resourceBaseModel);
        auditTaskService.putShelves(resourceBaseModel, this.getCurrentUserId(request));
        return R.ok().data(resourceBaseModel);

    }

    /**
     * 执行任务-审核操作
     *
     * @param executionTaskModel
     * @param request
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.AUDIT)
    @RequestMapping(value = "/auditTask",method = {RequestMethod.GET,RequestMethod.POST})
    public Object auditTask(ExecutionTaskModel executionTaskModel, HttpServletRequest request) {
        AuditTaskValidator.validAuditTask(executionTaskModel);
        return auditTaskService.auditTask(executionTaskModel, this.getUserInfo(request));

    }

    /**
     * 撤销审核
     *
     * @param resourceBaseModel
     * @param request
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.AUDIT)
    @RequestMapping(value = "/revokedAudit",method = {RequestMethod.GET,RequestMethod.POST})
    public Object revokedAudit(ResourceBaseModel resourceBaseModel, HttpServletRequest request) {
        AuditTaskValidator.validIdAndVersionAndType(resourceBaseModel);
        auditTaskService.revokedAudit(resourceBaseModel, this.getCurrentUserId(request));
        return R.ok();

    }

    /**
     * 审核历史信息
     *
     * @param resourceBaseModel
     * @param request
     * @return
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.AUDIT)
    @RequestMapping(value = "/historyList",method = {RequestMethod.GET,RequestMethod.POST})
    public Object auditHistoryList(ResourceBaseModel resourceBaseModel, HttpServletRequest request) {
        AuditTaskValidator.validIdAndVersionAndType(resourceBaseModel);
        List<AuditTaskInfoHistoryEntity> auditHistory = AuditUtil.queryAuditHistory(resourceBaseModel.getId(), resourceBaseModel.getVersion(), resourceBaseModel.getType());
        return render2Json(R.ok(auditHistory));
    }


}